﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using System.Windows.Controls;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace GuiTest
{
	[ConfigurationElementType(typeof(CustomTraceListenerData))]
	public class TextBoxTraceListener : CustomTraceListener
	{
		private static RichTextBox _logBox;

		internal static RichTextBox LogBox
		{
			get { return _logBox; }
			set { _logBox = value; }
		}

		public override void Write(string message)
		{
         _logBox.Dispatcher.Invoke(new Action<string>(_logBox.AppendText), message);			
		}

		public override void WriteLine(string message)
		{
			_logBox.AppendText(message+"\n");
		}

		public override void TraceData(TraceEventCache eventCache, string source,
		  TraceEventType eventType, int id, object data)
		{
			if (data is LogEntry && Formatter != null)
			{
				this.Write(Formatter.Format((LogEntry)data));
			}
		}
	}
}
